
package lt.andro.utils;

import lt.andro.seimorinkimai2012.BuildConfig;
import android.content.Context;
import android.util.Log;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

public class GoogleAnalyticsEventsHelper {
    public static final boolean TRACK_ALWAYS = true;
    public static final String TRACKING_ID = "UA-35526399-1";
    private static final String TAG = GoogleAnalyticsEventsHelper.class.getSimpleName();
    private static GoogleAnalyticsEventsHelper mMyGoogleAnalyticsHelper;
    private GoogleAnalyticsTracker mTracker;
    private static final int DISPATCH_INTERVAL = 13;// seconds

    public GoogleAnalyticsEventsHelper(Context context) {
        mTracker = GoogleAnalyticsTracker.getInstance();
        mTracker.startNewSession(TRACKING_ID, DISPATCH_INTERVAL, context);
    }

    public GoogleAnalyticsEventsHelper() {
    }

    public static GoogleAnalyticsEventsHelper getInstance(Context context) {
        if (mMyGoogleAnalyticsHelper == null) {
            mMyGoogleAnalyticsHelper = new GoogleAnalyticsEventsHelper(context);
        }
        return mMyGoogleAnalyticsHelper;
    }

    public static GoogleAnalyticsEventsHelper getInstance() {
        if (mMyGoogleAnalyticsHelper == null) {
            mMyGoogleAnalyticsHelper = new GoogleAnalyticsEventsHelper();
            Log.d(TAG,
                    "GAEH should be initialised with context. Creating instance, to save from failures.");
        }
        return mMyGoogleAnalyticsHelper;
    }

    public void trackEvent(String category, String action, String label, int value) {
        if (!BuildConfig.DEBUG || TRACK_ALWAYS) {
            Log.d(TAG, "Event: " + category + ", " + action + ", " + label + ", " + value + ".");
            mTracker.trackEvent(category, // Category
                    action, // Action
                    label, // Label
                    value); // Value
        }
    }

    public void dispatch() {
        Log.d(TAG, "Dispatch");
        mTracker.dispatch();
    }

    public void destroy() {
        Log.d(TAG, "destroy");
        mTracker.dispatch();
    }

    public void trackPageView(String page) {
        if (!BuildConfig.DEBUG || TRACK_ALWAYS) {
            Log.d(TAG, "Page: " + page);
            mTracker.trackPageView(page);
        }
    }
}
